<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 按身高和体重排队（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某学校举行运动会&#xff0c;学生们按编号(1、2、3…n)进行标识&#xff0c;现需要按照身高由低到高排列&#xff0c;对身高相同的人&#xff0c;按体重由轻到重排列&#xff1b;对于身高体重都相同的人&#xff0c;维持原有的编号顺序关系。请输出排列后的学生编号。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>两个序列&#xff0c;每个序列由n个正整数组成&#xff08;0 &lt; n &lt;&#61; 100&#xff09;。第一个序列中的数值代表身高&#xff0c;第二个序列中的数值代表体重。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>排列结果&#xff0c;每个数值都是原始序列中的学生编号&#xff0c;编号从1开始</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:62px;">输入</td><td style="width:436px;"> <p>4<br /> 100 100 120 130<br /> 40 30 60 50</p> </td></tr><tr><td style="width:62px;">输出</td><td style="width:436px;">2 1 3 4</td></tr><tr><td style="width:62px;">说明</td><td style="width:436px;"> <p>输出的第一个数字2表示此人原始编号为2&#xff0c;即身高为100&#xff0c;体重为30的这个人。</p> <p>由于他和编号为1的人身高一样&#xff0c;但体重更轻&#xff0c;因此要排在1前面。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:61px;">输入</td><td style="width:437px;"> <p>3<br /> 90 110 90<br /> 45 60 45</p> </td></tr><tr><td style="width:61px;">输出</td><td style="width:437px;">1 3 2</td></tr><tr><td style="width:61px;">说明</td><td style="width:437px;">1和3的身高体重都相同&#xff0c;需要按照原有位置关系让1排在3前面&#xff0c;而不是3 1 2。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>考察多条件排序。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    int[] heights &#61; new int[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) heights[i] &#61; sc.nextInt();

    int[] weights &#61; new int[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) weights[i] &#61; sc.nextInt();

    System.out.println(getResult(n, heights, weights));
  }

  public static String getResult(int n, int[] heights, int[] weights) {
    int[][] students &#61; new int[n][3];

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      students[i] &#61; new int[] {heights[i], weights[i], i &#43; 1};
    }

    Arrays.sort(
        students, (a, b) -&gt; a[0] !&#61; b[0] ? a[0] - b[0] : a[1] !&#61; b[1] ? a[1] - b[1] : a[2] - b[2]);

    StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
    for (int[] student : students) {
      sj.add(student[2] &#43; &#34;&#34;);
    }
    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 3) {
    let n &#61; parseInt(lines[0]);
    let heights &#61; lines[1].split(&#34; &#34;).map(Number);
    let weights &#61; lines[2].split(&#34; &#34;).map(Number);

    console.log(getResult(n, heights, weights));

    lines.length &#61; 0;
  }
});

function getResult(n, heights, weights) {
  let persons &#61; [];
  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    let p &#61; {};
    p.index &#61; i &#43; 1;
    p.height &#61; heights[i];
    p.weight &#61; weights[i];
    persons.push(p);
  }

  return persons
    .sort((a, b) &#61;&gt;
      a.height !&#61; b.height
        ? a.height - b.height
        : a.weight !&#61; b.weight
        ? a.weight - b.weight
        : a.index - b.index
    )
    .map((p) &#61;&gt; p.index)
    .join(&#34; &#34;);
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
heights &#61; list(map(int, input().split()))
weights &#61; list(map(int, input().split()))


# 算法入口
def getResult():
    students &#61; []

    for i in range(n):
        students.append([heights[i], weights[i], i &#43; 1])

    students.sort(key&#61;lambda x: (x[0], x[1], x[2]))

    return &#34; &#34;.join(map(lambda x: str(x[2]), students))


# 算法调用
print(getResult())
</code></pre> 
<p> </p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

typedef struct {
    int height;
    int weight;
    int idx;
} Student;

int cmp(const void* a, const void* b) {
    Student* A &#61; (Student*) a;
    Student* B &#61; (Student*) b;

    return A-&gt;height !&#61; B-&gt;height ? A-&gt;height - B-&gt;height : A-&gt;weight !&#61; B-&gt;weight ? A-&gt;weight - B-&gt;weight : A-&gt;idx - B-&gt;idx;
}

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    Student students[n];

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;students[i].height);
    }

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;students[i].weight);
        students[i].idx &#61; i &#43; 1;
    }


    qsort(students, n, sizeof(Student), cmp);

    char res[100000];

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        char tmp[1000];
        sprintf(tmp, &#34;%d&#34;, students[i].idx);

        strcat(res, tmp);

        if(i &lt; n - 1) {
            strcat(res, &#34; &#34;);
        }
    }

    puts(res);

    return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>